//
//  NotificationListViewModel.m
//  CommentPage
//
//  Created by 欧杨 on 2020/12/3.
//

#import "NotificationListViewModel.h"
#import "UserModel.h"
#import "Data.h"
#import "NotificationModel.h"
#import "NotificationCellViewModel.h"
#import <AFNetworking.h>

@interface NotificationListViewModel()

@property(assign, nonatomic) bool isReady;

@end

@implementation NotificationListViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.isReady = NO;
    }
    return self;
}

- (void)getNotifications
{
    self.isReady = NO;
    self.notifications = [NSMutableArray array];
    
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/notification/all"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in responseObject[@"Notification"]) {
            UserModel *user = [[UserModel alloc] initWithDictionary:dic[@"User"]];
            Data *data = [[Data alloc] initWithDictionary:dic[@"Data"]];
            NotificationModel *notification = [[NotificationModel alloc] initWithUser:user andData:data];
            NotificationCellViewModel *notificationViewModel = [[NotificationCellViewModel alloc] initWithNotificationModel:notification];
            [self.notifications addObject:notificationViewModel];
        }
        [self.notifications setArray:[[self.notifications reverseObjectEnumerator] allObjects]];
        self.isReady = YES;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed");
    }];
}

@end
